﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using FCBLL;
using MiscLib;

namespace FileConverter.BOMCompare
{
    public partial class Excel_Compare : System.Web.UI.Page
    {
        protected Dictionary<int, string> colorDic = new Dictionary<int, string>();
        ExcelCompare exc = new ExcelCompare();
        protected Dictionary<int, List<int>> redDic = new Dictionary<int, List<int>>();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            lblInfo.Text = string.Empty;
        }

        protected void Compare_Click(object sender, EventArgs e)
        {
            if (Excel_1.PostedFile.FileName == "" || Excel_1.PostedFile.InputStream.Length < 1)
            {
                lblInfo.Text = string.Empty;
                Session.Remove("Fullname");
                return;
            }
            if (Excel_2.PostedFile.FileName == "" || Excel_2.PostedFile.InputStream.Length < 1)
            {
                lblInfo.Text = string.Empty;
                Session.Remove("Fullname");
                return;
            }
            string Excel_1_File = Guid.NewGuid().ToString() + Path.GetExtension(Excel_1.FileName);
            string Excel_2_File = Guid.NewGuid().ToString() + Path.GetExtension(Excel_2.FileName);
            string Excel_1_FileName = new FileInfo(Excel_1.PostedFile.FileName).Name;
            string Excel_2_FileName = new FileInfo(Excel_2.PostedFile.FileName).Name;
            if (!Excel_1_File.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) && !Excel_1_File.EndsWith(".xls", StringComparison.OrdinalIgnoreCase))
            {
                lblInfo.Text = string.Empty;
                Session.Remove("Fullname");
                return;
            }
            if (!Excel_2_File.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) && !Excel_2_File.EndsWith(".xls", StringComparison.OrdinalIgnoreCase))
            {
                lblInfo.Text = string.Empty;
                Session.Remove("Fullname");
                return;
            }
            if (!Directory.Exists(Server.MapPath("~/Excel/")))
            {
                Directory.CreateDirectory(Server.MapPath("~/Excel/"));
            }
            Excel_1.PostedFile.SaveAs(Server.MapPath("~/Excel/") + Excel_1_File);
            Excel_2.PostedFile.SaveAs(Server.MapPath("~/Excel/") + Excel_2_File);
            int count_1 = 0;
            int count_2 = 0;
            DataTable dt_Excel_1 = ExcelExport.ImportExcel(Server.MapPath("~/Excel/") + Excel_1_File, out count_1, 0);
            DataTable dt_Excel_2 = ExcelExport.ImportExcel(Server.MapPath("~/Excel/") + Excel_2_File, out count_2, dt_Excel_1.Rows.Count);

            Dictionary<int, int> left_pos;
            Dictionary<int, int> right_pos;

            exc.Compare(dt_Excel_1, dt_Excel_2, out left_pos, out right_pos);

            dt_Excel_1 = exc.DataTableChange(dt_Excel_1, left_pos);
            dt_Excel_2 = exc.DataTableChange(dt_Excel_2, right_pos);

            colorDic = exc.Compared(dt_Excel_1, dt_Excel_2);
            redDic = exc.CompareDetail(colorDic, dt_Excel_1, dt_Excel_2);

            GridView_Excel_2.DataSource = dt_Excel_2;
            GridView_Excel_2.DataBind();
            GridView_Excel_1.DataSource = dt_Excel_1;
            GridView_Excel_1.DataBind();

            DataView dv = exc.ToDataView(dt_Excel_1, dt_Excel_2);

            System.Collections.Generic.List<string> numList = new List<string>();
            for (int i = 1; i <= dt_Excel_1.Rows.Count+2; i++)
            {
                numList.Add(i.ToString());
            }
            GridScroll.DataSource = numList;
            GridScroll.DataBind();

            string path = Server.MapPath("~/Excel/");
            ExporttoExcel(dv, path, Excel_1_FileName, Excel_2_FileName, colorDic, redDic);

        }

        protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            System.Drawing.Color red = System.Drawing.Color.FromArgb(50, 255, 106, 106);
            System.Drawing.Color green = System.Drawing.Color.FromArgb(50, 180, 238, 180);
            lblInfo.Text = "差异行数：" + (colorDic.Count);
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                if(colorDic.ContainsKey(e.Row.RowIndex))
                {
                    if (colorDic[e.Row.RowIndex].Equals("red"))
                    {
                        GridView_Excel_1.Rows[e.Row.RowIndex].BackColor = green;
                        GridView_Excel_2.Rows[e.Row.RowIndex].BackColor = green;
                        e.Row.BackColor = red;
                        List<int> li = redDic[e.Row.RowIndex];
                        foreach (int i in li)
                        {
                            GridView_Excel_1.Rows[e.Row.RowIndex].Cells[i].BackColor = red;
                            GridView_Excel_2.Rows[e.Row.RowIndex].Cells[i].BackColor = red;
                        }
                    }
                    else
                    {
                        e.Row.BackColor = green;
                        GridView_Excel_1.Rows[e.Row.RowIndex].BackColor = green;
                        GridView_Excel_2.Rows[e.Row.RowIndex].BackColor = green;
                    }
                }
            }
        }

        private void ExporttoExcel(DataView dv, string path, string filename_1, string filename_2, Dictionary<int, string> _colorDic, Dictionary<int, List<int>> _redDic)
        {
            Session["Fullname"] = exc.OutputExcel(dv, path, filename_1, filename_2, HttpContext.Current.Response, Server, _colorDic, _redDic);
        }

        protected void Download_Click(object sender, EventArgs e)
        {
            if (Session["Fullname"] == null || ((string)Session["Fullname"]).Equals(string.Empty)) return;
            ExcelExport.DownLoadFile((string)Session["Fullname"], HttpContext.Current.Response, Server);
            Session.Remove("Fullname");
        }
    }
}